﻿
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
var weburl='http://www.xeonconsulting.com/';
var siteurl;
$(document).ready(function () {
	siteurl=document.URL;
	//alert(siteurl);
	var index=siteurl.indexOf("public");
	//alert(index);
	if(index > 0)
	{
		weburl='https://xeonconsulting-public.sharepoint.com/';
	}
	GenerateTopMenu();
	GenerateActivities();
	LoadSlider();
	LoadIdeas();
	LoadWhatWeDo();

});

function LoadWhatWeDo() {
    var caml = "<Query>" +
                      "<Where>" +
                          "<Eq>" +
                              "<FieldRef Name='key' /><Value Type='Text'>whatwedo</Value>" +
                          "</Eq>" +
                      "</Where>" +
                  "</Query>";

    var html = ""
    $().SPServices({
        operation: 'GetListItems',
        async: false,
        webURL: weburl,
        listName: 'ConfigStore',
        CAMLRowLimit: 1,
        CAMLQuery: caml,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='url' /><FieldRef Name='description' /><FieldRef Name='key' /></ViewFields>",
        completefunc: function (xData, Status) {
            $(jQuery.parseXML(xData.responseText)).SPFilterNode("z:row").each(function () {
                var title = $(this).attr('ows_Title');
                var url = $(this).attr('ows_url');
                var description = $(this).attr('ows_description');
                html += "<h2>" + title + "</h2>";

                html += description;                
            });
            $("#whatwedo").html(html);
        }
    });
}

function LoadIdeas() {
    var html = "<h2>RECENT ACTIVITIES</h2><ul>"
    $().SPServices({
        operation: 'GetListItems',
        async: false,
        webURL: weburl,
        listName: 'FreshIdeas',
        CAMLRowLimit: 9,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='url' /></ViewFields>",
        completefunc: function (xData, Status) {
            $(jQuery.parseXML(xData.responseText)).SPFilterNode("z:row").each(function () {
                var title = $(this).attr('ows_Title');
                var url = $(this).attr('ows_url');
                html    +=  "<li><a href=\""+url+"\">"+title+"</a></li>"
            });
            html +="</ul>"
            $("#freshideas").html(html);
        }
    });
}


function LoadSlider() {
    var slidercaml = "<Query>" +
                      "<Where>" +
                          "<Eq>" +
                              "<FieldRef Name='ImageType' /><Value Type='Text'>Slider</Value>" +
                          "</Eq>" +
                      "</Where>" +
                  "</Query>";
    var sliderhtml = "<ul class=\"slides\">"
    $().SPServices({
        operation: 'GetListItems',
        async: false,        
        webURL: weburl,
        listName: 'Photos',
        CAMLRowLimit: 9,
        CAMLQuery: slidercaml,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Description' /><FieldRef Name='Keywords' /><FieldRef Name='url' /></ViewFields>",
        completefunc: function (xData, Status) {
            $(jQuery.parseXML(xData.responseText)).SPFilterNode("z:row").each(function () {
                var imagename = $(this).attr('ows_FileLeafRef');
                imagename = imagename.split("#")[1];
                var title = $(this).attr('ows_Title');
                var keywords = $(this).attr('ows_Keywords');
                var description = $(this).attr('ows_Description');
                var imageurl = weburl+"/Lists/Photos/" + imagename;
		//alert(imageurl);
                var url = $(this).attr('ows_url');
                sliderhtml += "<li><p class=\"slidercaption\"></p><p class=\"sliderkeyword\">"+keywords+"</p><p class=\"sliderdescription\">"+description+"</p><img src=\""+imageurl+"\"\" alt=\"\"/></li>";
            });
            sliderhtml += "</ul>";
            $("#idslider").html(sliderhtml);
            //simple FlexSlider call
            $('.flexslider').flexslider();

            // more complex call
            $('.flexslider').flexslider({
                animation: "slide",
                controlsContainer: ".flex-container",
                start: function (slider) {
                    $('.total-slides').text(slider.count);
                },
                after: function (slider) {
                    $('.current-slide').text(slider.currentSlide);
                }
            });
        }

    });

}
function GenerateActivities() {
    var caml = "<Query>" +
                      "<Where>" +
                          "<Eq>" +
                              "<FieldRef Name='ImageType' /><Value Type='Text'>Activities</Value>" +
                          "</Eq>" +
                      "</Where>" +
                  "</Query>";
    var html = "<div class=\"project-group\"><table><tbody><tr>"
    $().SPServices({
        operation: 'GetListItems',
        async: false,
        webURL: weburl,
        listName: 'Photos',
        CAMLRowLimit: 9,
        CAMLQuery: caml,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Summary' /><FieldRef Name='url' /></ViewFields>",
        completefunc: function (xData, Status) {
            $(jQuery.parseXML(xData.responseText)).SPFilterNode("z:row").each(function () {
                var imagename = $(this).attr('ows_FileLeafRef');
                imagename = imagename.split("#")[1];
                var title = $(this).attr('ows_Title');
                var description = $(this).attr('ows_Summary');
                var imageurl = "http://www.xeonconsulting.com/Lists/Photos/" + imagename;
                var url = $(this).attr('ows_url');
                html += "<td><div><img src=\""+imageurl+"\"\" alt=\"\"/></div><div><img src=\"/SiteAssets/line.png\" alt=\"\"/></div><div><h3>"+title+"</h3></div><div style=\"text-align:left;padding-left:10px;\">"+description+"</td>";
            });
            html += "</tr></tbody></table></div>";
            $("#idcontainer").html(html);
        }
        
    });

}


function GenerateTopMenu() {
    var menucaml = "<Query>" +
                      "<OrderBy>" +
                              "<FieldRef Name='SortOrder' Ascending='True' />" +
                      "</OrderBy>" +
                  "</Query>";
	var menuList=[];
    $().SPServices({
    operation: 'GetListItems',
    async: false,
    webURL: weburl,
    listName: 'Top Menu',
    CAMLQuery: menucaml,
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Parent' /><FieldRef Name='SortOrder' /><FieldRef Name='url' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(jQuery.parseXML(xData.responseText)).SPFilterNode("z:row").each(function() {
		var parent=null;		
		if($(this).attr('ows_Parent'))
			parent=$(this).attr('ows_Parent');
        menuList.push({
	            'title': $(this).attr('ows_Title'),
	            'parent': parent,
	            'url': $(this).attr('ows_url')
            });       
      });
}});              
           
        
        var menulevel1= [];

		$(menuList).each(function(){
		 if(this.parent==null)
		   menulevel1.push(this);
		});
		
		var html = '<ul>';
	$(menulevel1).each(function(){
	    var submenu = '<div class="submenu"><ul>';
		  var title=this.title;
		  var url=this.url;
		  var imageHtml='';		  
		  $(menuList).each(function(){		  	
			 if(this.parent == title){
			   		submenu=submenu+'<li><a href="'+ this.url +'">'+ this.title +'</a></li>';			   
			   }
			});
		
		  if (submenu !== '<div class="submenu"><ul>')
		      submenu = submenu + '</ul></div>';
		  else
			submenu='';
		if(!url)
			url="#";
		html +='<li><a href="'+url+'">'+ this.title +'</a>' + submenu +'</li>';		
	   	   	
	});
		
	$('#topmenu').html(html + '</ul>')	
   }
   

